/*
 * @Description: 用户模块
 * @Version: 1.0
 * @Autor: JiaJun Wu
 * @Date: 2021-11-04 17:45:32
 * @LastEditors: jiajun wu
 * @LastEditTime: 2021-12-06 15:39:05
 */
import { Module } from "vuex";
import { RootStateInterface } from "./rootStateInterface";
import { getToken } from "@/utils/util";
import { isAuthed, login } from "@/api/system-service";
import { Cookie, LocalStorage, SessionStorage } from "@/utils/storage";
import { LoginFormStateStruct, LoginStruct, UserInfoStruct } from "@/interface/user";
import { ACCESS_TOKEN } from "../mutation-types";

export interface UserState {
  /** 用户信息 */
  userInfo: UserInfoStruct | undefined
  /** 头像 */
  avatar: string | undefined
}

const user: Module<UserState, RootStateInterface> = {
  namespaced: false, // 防止模块命名冲突 设置后调用mutations和action需要'模块名/方法' 例如  user/getToken
  state: {
    userInfo: undefined,
    avatar: undefined,
  },
  getters: {
    userInfo: state => state.userInfo
  },
  mutations: {
    SET_INFO: (state, info) => {
      state.userInfo = info
      if (info) {
        state.avatar = info.photo || '/avatar2.jpg'
        // state.welcome = welcome()
      }
    },
  },
  actions: {
    isAuthed({ commit }) {
      return new Promise((resolve, reject) => {
        const token = getToken()
        if (token) {
          isAuthed()
            .then(res => {
              let result = res.data
              if (result.success) {
                commit('SET_INFO', result.body)
                resolve(result.body)
              } else {
                commit('SET_INFO', undefined)
                commit('app/SET_TOKEN', null)
                LocalStorage.clear()
                SessionStorage.clear()
                Cookie.clear()
                reject()
              }
            }).catch(() => {
              commit('SET_INFO', undefined)
              commit('app/SET_TOKEN', null)
              LocalStorage.clear()
              SessionStorage.clear()
              Cookie.clear()
              reject()
            })
        } else {
          commit('SET_INFO', undefined)
          commit('app/SET_TOKEN', null)
          LocalStorage.clear()
          SessionStorage.clear()
          Cookie.clear()
          reject()
        }
      })
    },
    login({ commit }, userInfo: LoginFormStateStruct) {
      return new Promise((resolve, reject) => {
        let data: LoginStruct = {
          username: userInfo.username,
          password: userInfo.password
        };
        login(data).then(res => {
          let result = res.data
          if (result.success) {
            LocalStorage.set(ACCESS_TOKEN, result.body.token, 7 * 24 * 60 * 60 * 1000)
            SessionStorage.set(ACCESS_TOKEN, result.body.token, 7 * 24 * 60 * 60 * 1000)
            commit('SET_INFO', result.body.userInfo)
            commit('SET_TOKEN', result.body.token)
            resolve(result.body.userInfo)
          } else {
            reject()
          }
        }).catch(error => {
          reject()
        })
      })
    },
    Logout({ commit }) {
      return new Promise((resolve, reject) => {
        LocalStorage.clear()
        SessionStorage.clear()
        Cookie.clear()
      })
    }
  },
}

export default user
